package v1

import (
	"gin-vue-admin/global"
	"gin-vue-admin/model"
	"gin-vue-admin/model/request"
	"gin-vue-admin/model/response"
	"gin-vue-admin/service"
	"github.com/gin-gonic/gin"
	"github.com/gin-gonic/gin/binding"
	"go.uber.org/zap"
	"strconv"
	"time"
)

// @Tags UserProject
// @Summary 创建UserProject
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body model.UserProject true "创建UserProject"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /userProject/createUserProject [post]
func CreateUserProject(c *gin.Context) {
	var userProject model.UserProject
	_ = c.ShouldBindJSON(&userProject)
	if err := service.CreateUserProject(userProject); err != nil {
        global.GVA_LOG.Error("创建失败!", zap.Any("err", err))
		response.FailWithMessage("创建失败", c)
	} else {
		response.OkWithMessage("创建成功", c)
	}
}

// @Tags UserProject
// @Summary 删除UserProject
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body model.UserProject true "删除UserProject"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"删除成功"}"
// @Router /userProject/deleteUserProject [delete]
func DeleteUserProject(c *gin.Context) {
	var userProject model.UserProject
	_ = c.ShouldBindJSON(&userProject)
	if err := service.DeleteUserProject(userProject); err != nil {
        global.GVA_LOG.Error("删除失败!", zap.Any("err", err))
		response.FailWithMessage("删除失败", c)
	} else {
		response.OkWithMessage("删除成功", c)
	}
}

// @Tags UserProject
// @Summary 批量删除UserProject
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body request.IdsReq true "批量删除UserProject"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"批量删除成功"}"
// @Router /userProject/deleteUserProjectByIds [delete]
func DeleteUserProjectByIds(c *gin.Context) {
	var IDS request.IdsReq
    _ = c.ShouldBindJSON(&IDS)
	if err := service.DeleteUserProjectByIds(IDS); err != nil {
        global.GVA_LOG.Error("批量删除失败!", zap.Any("err", err))
		response.FailWithMessage("批量删除失败", c)
	} else {
		response.OkWithMessage("批量删除成功", c)
	}
}

// @Tags UserProject
// @Summary 更新UserProject
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body model.UserProject true "更新UserProject"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"更新成功"}"
// @Router /userProject/updateUserProject [put]
type AgreeStruct struct {
	AgreeTime  time.Time `json:"agreeTime"`
}
func UpdateUserProject(c *gin.Context) {
	var userProject model.UserProject
	_ = c.ShouldBindBodyWith(&userProject,binding.JSON)
	if userProject.NowStatus == 2 {
		userProject.AgreeTime.Time = time.Now()
		userProject.AgreeTime.Valid  = true
	}
	if err := service.UpdateUserProject(userProject); err != nil {
        global.GVA_LOG.Error("更新失败!", zap.Any("err", err))
		response.FailWithMessage("更新失败", c)
	} else {
		response.OkWithMessage("更新成功", c)
	}
}

// @Tags UserProject
// @Summary 用id查询UserProject
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body model.UserProject true "用id查询UserProject"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"查询成功"}"
// @Router /userProject/findUserProject [get]
func FindUserProject(c *gin.Context) {
	var userProject model.UserProject
	_ = c.ShouldBindQuery(&userProject)
	if err, reuserProject := service.GetUserProject(userProject.ID); err != nil {
        global.GVA_LOG.Error("查询失败!", zap.Any("err", err))
		response.FailWithMessage("查询失败", c)
	} else {
		response.OkWithData(gin.H{"reuserProject": reuserProject}, c)
	}
}

// @Tags UserProject
// @Summary 分页获取UserProject列表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body request.UserProjectSearch true "分页获取UserProject列表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /userProject/getUserProjectList [get]
func GetUserProjectList(c *gin.Context) {
	var pageInfo request.UserProjectSearch
	_ = c.ShouldBindQuery(&pageInfo)
	types,_:= strconv.Atoi(c.Query(`type`))

	if err, list, total := service.GetUserProjectInfoList(pageInfo,types); err != nil {
	    global.GVA_LOG.Error("获取失败!", zap.Any("err", err))
        response.FailWithMessage("获取失败", c)
    } else {
		allCount ,applyCount ,overCount  := service.UserProjectTotal()
        response.OkWithDetailed(response.PageResult{
            List:     list,
            Total:    total,
            Page:     pageInfo.Page,
            PageSize: pageInfo.PageSize,
			AllCount: allCount,
			ApplyCount: applyCount,
			OverCount: overCount,
        }, "获取成功", c)
    }
}
